package cn.lanqiao.springbootproject.controller;

import cn.lanqiao.springbootproject.service.ProductService;
import cn.lanqiao.springbootproject.util.Result;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    AmqpTemplate amqpTemplate;

    @GetMapping("/kill/{killId}/{userId}")
    public Result killProduct(@PathVariable int killId, @PathVariable int userId) {
        long ret = productService.killProductLua(userId, killId);
        if (ret == 1)
            return Result.fail(700, "不能重复秒杀！");
        else if (ret == 2)
            return Result.fail(701, "库存不足！");
        return Result.success();
    }

    @GetMapping("/send/{key}")
    public Result send(@PathVariable String key){
        for (int i = 0; i < 10; i++) {
            amqpTemplate.convertAndSend("directExchange",key,"hello - " + i);
        }

        return Result.success();
    }


}
